package com.yyfax.account.common.util;

import com.jaws.core.common.util.DateUtil;
import com.yyfax.account.common.exception.AccountExceptionCode;
import com.yyfax.account.vo.JobContext;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * JobContext工具
 *
 * @author yangrd
 * @date 2017-10-23 14:15:00
 * @since v1.0.0
 */
public class JobContextUtils {

	private static DateTimeFormatter formatter = DateTimeFormat.forPattern(DateUtil.FORMAT_DATE);

	/**
	 * 获取日切时间
	 *
	 * @author maojian
	 * @date 2017-09-05 13:40:12
	 * @since v1.0.0
	 * @param context job上下文
	 * @return DateTime
	 */
	public static DateTime getJobDay(JobContext context) {
		if (null == context || null == context.getParam("args")) {
			return getNow();
		}
		String dateStr = (String) context.getParam("args");
		if (!DateUtil.isFormat(dateStr, DateUtil.FORMAT_DATE)) {
			throw AccountExceptionCode.JOB_DATE_FORMAT_ERROR.exp(dateStr);
		}
		DateTime jobDay = DateTime.parse(dateStr, formatter);
		if (jobDay.isAfter(getNow())) {
			throw AccountExceptionCode.JOB_DATE_ERROR.exp(dateStr);
		}
		return jobDay;
	}

	/**
	 * 获取当天凌晨时间
	 * 
	 * @author maojian
	 * @date 2017-11-02 11:12:00
	 * @since v1.0.0
	 * @return
	 */
	private static DateTime getNow() {
		return DateTime.now().toDateMidnight().toDateTime();
	}
}
